package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"weekly/model/cache"
	"weekly/weeklyrpc/internal/svc"
	"weekly/weeklyrpc/weeklyrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *weeklyrpc.SendSmsRequest) (*weeklyrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Account).Result()
	if result >= "1" {
		return nil, fmt.Errorf("一分钟只能发送一次")
	}

	code := rand.Intn(900000) + 100000

	//sms, _ := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}

	cache.Rdb.Set(l.ctx, "SendSms"+in.Account, code, time.Minute*5)

	cache.Rdb.Incr(l.ctx, "sendSms"+in.Account)

	cache.Rdb.Expire(l.ctx, "sendSms"+in.Account, time.Minute*1)

	return &weeklyrpc.SendSmsResponse{}, nil
}
